package com.minhua.jijiehao2.mainframe.common.webviewactivity.impl

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
import android.view.View
import android.webkit.*
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.mainframe.common.webviewactivity.imp.IWebView
import kotlinx.android.synthetic.main.activity_web.*

class WebActivity : BaseActivity<IWebView, WebPresenter>(), IWebView {

    override fun getLayoutId(): Int = R.layout.activity_web

    override fun getPresenter(): WebPresenter = WebPresenter()

    private val mOnClickListener = View.OnClickListener { v ->
        when (v.id) {
            else -> {
            }
        }
    }

    companion object {
        fun intentStart(context: Context, url: String, title: String) {
            val intent = Intent(context, WebActivity::class.java)
            intent.putExtra("url", url)
            intent.putExtra("title", title)
            context.startActivity(intent)
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun initView() {
        val settings = wvWeb.settings
        //支持缩放，默认为true。
        settings.setSupportZoom(false)
        //调整图片至适合webview的大小
        settings.useWideViewPort = true
        settings.javaScriptEnabled = true
        // 缩放至屏幕的大小
        settings.loadWithOverviewMode = true
        //设置默认编码
        settings.defaultTextEncodingName = "utf-8"
        //设置自动加载图片
        settings.loadsImagesAutomatically = true
        //支持通过JS打开新窗口
        settings.javaScriptCanOpenWindowsAutomatically = true
        //支持内容重新布局
        settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        //关闭webview中缓存
        settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK

        wvWeb.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                pbProgress.visibility = View.INVISIBLE

                Log.d("url", "finish loading :" + url)
            }
        }
        wvWeb.webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                if (newProgress == 100) {
                    pbProgress.visibility = View.INVISIBLE
                }
                pbProgress.progress = newProgress
            }
        }

        wvWeb.setDownloadListener { url, _, _, _, _ ->
            //调用系统浏览器下载
            val uri = Uri.parse(url)
            val intent = Intent(Intent.ACTION_VIEW, uri)
            startActivity(intent)
        }

        val urlString = intent.getStringExtra("url")
        val titleString = intent.getStringExtra("title")
        ctTitle.setTitle(titleString)
        wvWeb.loadUrl(urlString)
    }

    override fun onResume() {
        super.onResume()
        wvWeb.onResume()
    }

    override fun onPause() {
        super.onPause()
        wvWeb.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        wvWeb.destroy()
    }
}
